package test.bingo;

import java.util.ArrayList;
import java.util.Collections;

import com.opensymphony.xwork2.ActionSupport;

public class Bingo extends ActionSupport {
	ArrayList<BingoPan> list;
	BingoPan pan;
	String checknum;
	ArrayList<Ranking> ranking;
	int player1;
	int player2;
	int player3;
	int player4;
	
	public String inputNum()throws Exception{
		BingoDAO dao = new BingoDAO();
		String player = pan.name;
		
		System.out.println("inputNum start!" + list);
		System.out.println(pan);
		list = dao.getPan();
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).getNum() == pan.getNum() && list.get(i).getName() != null){
				System.out.println("overlap.get(i).getNum() : " + list.get(i).getNum());
				score();
				return player;
			}
		}
		
		dao.insertNum(pan);
		list = dao.getPan();
		for (int i = 0; i < 4; i++) {
			
		}
		
		score();
		
		int check= check();
		System.out.println("victory : " + check);
		
		if(check>4){
			dao.delete();
			return "victory";
		}
		
		
		if(pan.name.equals("player1")){
			return "player2";
		}else if(pan.name.equals("player2")){
			return "player3";
		}else if(pan.name.equals("player3")){
			return "player4";
		}else if(pan.name.equals("player4")){
			return "player1";
		}
	
		return SUCCESS;
	}
	

	public void score()throws Exception{
		BingoDAO dao = new BingoDAO();
		list = dao.getPan();
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).name != null){
				if(list.get(i).name.equals("player1")){
					player1 +=list.get(i).score; 
				}
				if(list.get(i).name.equals("player2")){
					player2 +=list.get(i).score; 
				}
				if(list.get(i).name.equals("player3")){
					player3 +=list.get(i).score; 
				}
				if(list.get(i).name.equals("player4")){
					player4 +=list.get(i).score; 
				}
			}
		}
		
		
		ranking = dao.getScore();
		
		
		
		System.out.println(ranking);		
	}
	
	public String createPan()throws Exception{
		BingoDAO dao = new BingoDAO();
		System.out.println("createPan start!");
		dao.createPan();
		list = dao.getPan();
		return SUCCESS;
	}
	
	public int check(){
		
		int rowCount = 0;		
		int colCount = 0;
		
		int count1=0;
		int count2=0;
		
		
		for(int i=0; i<25; i+=5) {			
			int count = 0;
			for(int j=i; j<i+5; j++) {
				
				if(list.get(j).name != null) {
					
					count++;
					if(count >4){
						rowCount++;
					}
				}
			}
		}

		for(int i=0; i<5; i++) {			
			int count = 0;
			for(int j=i; j<25; j+=5) {
			
				if(list.get(j).name != null) {
					
					count++;
					if(count >4){
						colCount++;
					}
				}
			}
		}
		
		int count3=0;
		for(int i=0; i<25; i+=6) {	
			
			if(list.get(i).name != null) {
				count3++;
				if(count3 >4){
					count1++;
				}
				
				
			}
		}
		int count4=0;
		for(int i=4; i<24; i+=4) {	
			
			if(list.get(i).name != null) {
				count4++;
				if(count4 >4){
					count2++;
				}
			}
		}
		
		
		int checkk = rowCount+colCount+count1+count2;
		
		return checkk;

	}

	public ArrayList<BingoPan> getList() {
		return list;
	}

	public void setList(ArrayList<BingoPan> list) {
		this.list = list;
	}

	public BingoPan getPan() {
		return pan;
	}

	public void setPan(BingoPan pan) {
		this.pan = pan;
	}

	public String getChecknum() {
		return checknum;
	}

	public void setChecknum(String checknum) {
		this.checknum = checknum;
	}

	public int getPlayer1() {
		return player1;
	}

	public void setPlayer1(int player1) {
		this.player1 = player1;
	}

	public int getPlayer2() {
		return player2;
	}

	public void setPlayer2(int player2) {
		this.player2 = player2;
	}

	public int getPlayer3() {
		return player3;
	}

	public void setPlayer3(int player3) {
		this.player3 = player3;
	}

	public int getPlayer4() {
		return player4;
	}

	public void setPlayer4(int player4) {
		this.player4 = player4;
	}

	public ArrayList<Ranking> getRanking() {
		return ranking;
	}

	public void setRanking(ArrayList<Ranking> ranking) {
		this.ranking = ranking;
	}


	
}
